package com.uenpay.im.ui.old.activity.chat;

import android.text.TextUtils;
import android.util.Log;

import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.callback.socket.ISendMessageListener;
import com.qyx.android.callback.socket.IUploadAttacResultListener;
import com.qyx.android.message.type.MsgContentType;
import com.qyx.android.protocol.BaseContentModel;
import com.qyx.android.protocol.QyxMsg;
import com.qyx.android.protocol.SendMsgCallBack;
import com.qyx.android.protocol.SessionModel;
import com.uenpay.im.IMApplication;
import com.uenpay.im.entities.SendFileResult;
import com.uenpay.im.entities.chatbean.AttachModel;
import com.uenpay.im.entities.chatbean.MsgVideoModel;
import com.uenpay.im.invokeitems.chat.OnAttachedListener;
import com.uenpay.im.invokeitems.chat.UploadMessageAttachment;

import java.io.File;
import java.io.IOException;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @date 2016-5-21 下午7:15:36
 * @function 发送消息管理
 */
public class SendMsgHandle {

	private static final String TAG = "SendMsgHandle";

	public interface ISendMsgCallBack {
		void onSendMsgResult(int sendMsgResult, QyxMsg qyxMsg);
	}

	public interface IUploadAttachFaild {
		void onUploadFailed(String msg_no);
	}

	/**
	 * 发送消息
	 *
	 * @param baseContentModel
	 * @param mISendMsgCallBack
	 */
	public static void sendMsg(BaseContentModel baseContentModel, final SessionModel sessionModel,
			final ISendMsgCallBack mISendMsgCallBack, final IUploadAttachFaild uploadAttachFaild) {
		Log.d(TAG, "send Msg " + baseContentModel.toString());
		SendMsgCallBack callBack = new SendMsgCallBack();
		callBack.iSendMessageListener = new ISendMessageListener() {

			@Override
			public void onSendMsgCallBack(int sendMsgResult, QyxMsg qyxMsg) {
				// TODO
				if (mISendMsgCallBack != null)
					mISendMsgCallBack.onSendMsgResult(sendMsgResult, qyxMsg);
			}

			@Override
			public void onUploadAttachmentListener(final QyxMsg qyxMsg, final IUploadAttacResultListener attacResultListener) {
				if (qyxMsg != null && qyxMsg.baseContentModel != null && qyxMsg.baseContentModel.getAttachmentModel() != null) {
					// 上传附件
					AttachModel attachModel = (AttachModel) qyxMsg.baseContentModel.getAttachmentModel();
					if (qyxMsg.msg_content_type == MsgContentType.VIDEO) {
						// TODO: 2017/2/17 视频
						final MsgVideoModel msgVideoModel = (MsgVideoModel) qyxMsg.baseContentModel;
						if (TextUtils.isEmpty(msgVideoModel.getScreeHostFileId())
								|| TextUtils.isEmpty(msgVideoModel.getScreeHostFileHash())) {
							// 视频封面未上传
							if (!TextUtils.isEmpty(msgVideoModel.getScreeHostLocalPath())) {
								uploadMsgAttach(msgVideoModel.getScreeHostLocalPath(), qyxMsg.sessionModel.getSessionId() + "",
										new OnAttachedListener() {
											@Override
											public void onAttached(SendFileResult sendResult, boolean isFailed) {
												boolean is_success = true;
												final AttachModel mAttachModel = (AttachModel) qyxMsg.baseContentModel.getAttachmentModel();
												if (TextUtils.isEmpty(sendResult.getFileHash())
														|| TextUtils.isEmpty(sendResult.getFileId())) {
													is_success = false;
													uploadAttachFaild.onUploadFailed(qyxMsg.msg_no);
													attacResultListener.onUploadResult(is_success, mAttachModel);
												} else {
													// 上传封面成功
													msgVideoModel.setScreeHostFileHash(sendResult.getFileHash());
													msgVideoModel.setScreeHostFileId(sendResult.getFileId());
													qyxMsg.baseContentModel = msgVideoModel;
													/** 上传视频文件 **/
													uploadAttach(qyxMsg, attacResultListener, uploadAttachFaild);
												}
											}
										});
							}
						}
					} else {
						/** 如果该文件已上传过，直接发送消息 **/
						if (!TextUtils.isEmpty(attachModel.getFileId()) && !TextUtils.isEmpty(attachModel.getFileHash())) {
							attacResultListener.onUploadResult(true, attachModel);
						} else {
							uploadAttach(qyxMsg, attacResultListener, uploadAttachFaild);
						}
					}

				}
			}
		};
		IMApplication.getInstance().sendMessage(baseContentModel, sessionModel, callBack);
	}

	/**
	 * 上传聊天附件
	 *
	 * @param file_path
	 * @param chat_id
	 * @param onAttachedListener
	 */
	public static void uploadMsgAttach(String file_path, String chat_id, OnAttachedListener onAttachedListener) {
		UploadMessageAttachment(file_path, onAttachedListener);
	}

	private static void uploadAttach(final QyxMsg qyxMsg, final IUploadAttacResultListener attacResultListener,
			final IUploadAttachFaild uploadAttachFaild) {
		final AttachModel mAttachModel = (AttachModel) qyxMsg.baseContentModel.getAttachmentModel();
		UploadMessageAttachment(mAttachModel.getFileLocalPath(), new OnAttachedListener() {

			@Override
			public void onAttached(SendFileResult sendResult, boolean isFailed) {
				mAttachModel.setFileHash(sendResult.getFileHash());
				mAttachModel.setFileId(sendResult.getFileId());
				boolean is_success = true;
				if (TextUtils.isEmpty(sendResult.getFileHash()) || TextUtils.isEmpty(sendResult.getFileId())) {
					is_success = false;
					uploadAttachFaild.onUploadFailed(qyxMsg.msg_no);
				}
				attacResultListener.onUploadResult(is_success, mAttachModel);
			}
		});
	}

	/**
	 * 上传消息中的附件
	 *
	 * @param file_path
	 * @param onAttachedListener
	 */
	private static void UploadMessageAttachment(String file_path, OnAttachedListener onAttachedListener) {
		if (TextUtils.isEmpty(file_path)) {
			return;
		}

		byte[] file_bytes = null;
		// 保存录音在本地数据库中
		File file = new File(file_path);
		try {
			file_bytes = Utils.getBytesFromFile(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String[] arrs = null;
		try {
			arrs = file_path.split("\\.");// 后缀名
			if (arrs.length < 2) {
				arrs = file_path.split("\\/");// 后缀名
			}
		} catch (Exception e) {
			arrs[0] = "log";
			arrs[1] = "error";
		}

		new UploadMessageAttachment(file_bytes, arrs[arrs.length - 1], IMApplication.getAppContext(), onAttachedListener);

	}
}
